<?php
	$whmcs_path="whmcs";
	require($_SERVER['DOCUMENT_ROOT']."/".$whmcs_path."/dbconnect.php");
	
	$server = $_POST['txtID'];
	$comments = str_replace("'", "\'", $_POST['txtComments']);
	
	//check server log is a new log, if new, update the null comment and later times, just save log with the login user
	$query = "SELECT sid, ip  FROM mod_serverlog WHERE comments is null AND (sid = '".$server."' OR ip = '".$server."')";
	$rs = mysql_query($query);
	$count = mysql_num_rows($rs);	//count equal to 1 mean the log in new added -> do update action!
	
	if($count==1){
		$row = mysql_fetch_array($rs);
	 	$query = "UPDATE mod_serverlog  set modifiedby = ".$_SESSION['adminid'].", comments = '".$comments."' WHERE sid ='".$server."' OR ip ='".$server."'";
		mysql_query($query);
	}else{
		//get infor
		$query = "SELECT ip, domain, createdby FROM mod_serverlog WHERE sid = '".$server. "' OR ip ='" . $server ."' LIMIT 1";
		$rs = mysql_query($query);
		$row = mysql_fetch_array($rs);
		//insert data
		 $query = "INSERT INTO mod_serverlog(ip, domain, createdby, modifiedby, comments) 
				VALUES('".$row['ip']."','".$row['domain']."','".$row['createdby']."','".$_SESSION['adminid']."','".$comments."')";
		mysql_query($query);	
	}
	//echo $query;
	
	$path = "http://" . $_SERVER['SERVER_NAME'] . "/whmcs/admin/addonmodules.php?module=server_log&serverID=".$server."#log";
	header("location: ".$path);
	
?>
